package com.yugao.fintech.antelope.pay.payment.channel.wxpay;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yugao.fintech.antelope.pay.common.utils.AmountUtils;
import com.yugao.fintech.antelope.pay.constant.IfCodeEnum;
import com.yugao.fintech.antelope.pay.model.entity.PayOrder;
import com.yugao.fintech.antelope.pay.payment.channel.AbstractPaymentService;
import com.yugao.fintech.antelope.pay.payment.model.bo.MchAppConfigContext;
import com.yugao.fintech.antelope.pay.payment.model.params.wxpay.WxpayIsvsubMchParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 支付接口： 微信官方
 *
 
 */
@Service
public abstract class WxpayPaymentService extends AbstractPaymentService {

    @Override
    public IfCodeEnum getIfCode() {
        return IfCodeEnum.WXPAY;
    }

    @Override
    public boolean isSupport(String wayCode) {
        return true;
    }

//    /**
//     * 构建微信统一下单请求数据
//     */
//    public WxPayUnifiedOrderRequest buildUnifiedOrderRequest(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) {
//        String payOrderId = payOrder.getPayOrderId();
//
//        // 微信统一下单请求对象
//        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
//        request.setOutTradeNo(payOrderId);
//        request.setBody(payOrder.getSubject());
//        request.setDetail(payOrder.getBody());
//        request.setFeeType("CNY");
//        request.setTotalFee(payOrder.getAmount().intValue());
//        request.setSpbillCreateIp(payOrder.getClientIp());
//        request.setNotifyUrl(getNotifyUrl());
//        request.setProductId(System.currentTimeMillis()+"");
//        request.setTimeExpire(DateUtil.format(payOrder.getExpiredTime(), DatePattern.PURE_DATETIME_PATTERN));
//
//        //订单分账， 将冻结商户资金。
//        if(isDivisionOrder(payOrder)){
//            request.setProfitSharing("Y");
//        }
//
//        // 特约商户
//        if(mchAppConfigContext.isIsvsubMch()){
//            WxpayIsvsubMchParams isvsubMchParams = (WxpayIsvsubMchParams) configContextQueryService.queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), getIfCode());
//            request.setSubMchId(isvsubMchParams.getSubMchId());
//            if (StringUtils.isNotBlank(isvsubMchParams.getSubMchAppId())) {
//                request.setSubAppId(isvsubMchParams.getSubMchAppId());
//            }
//        }
//
//        return request;
//    }

    /**
     * 构建微信APIV3接口  统一下单请求数据
     */
    public JSONObject buildV3OrderRequest(WxPayService wxPayService, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) {
        String payOrderNo = payOrder.getPayOrderNo();

        // 微信统一下单请求对象
        JSONObject reqJSON = new JSONObject();
        reqJSON.put("out_trade_no", payOrderNo);
        reqJSON.put("description", payOrder.getGoodsSubject());
        // 订单失效时间，遵循rfc3339标准格式，格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,示例值：2018-06-08T10:34:56+08:00
        reqJSON.put("time_expire", String.format("%sT%s+08:00", DateUtil.format(payOrder.getExpiredTime(), DatePattern.NORM_DATE_FORMAT), DateUtil.format(payOrder.getExpiredTime(), DatePattern.NORM_TIME_FORMAT)));

        reqJSON.put("notify_url", getNotifyUrl(payOrderNo));

        JSONObject amount = new JSONObject();
        amount.put("total", AmountUtils.yuanToFen(payOrder.getPayAmount()).intValue());
        amount.put("currency", "CNY");
        reqJSON.put("amount", amount);

        JSONObject sceneInfo = new JSONObject();
        sceneInfo.put("payer_client_ip", payOrder.getClientIp());
        reqJSON.put("scene_info", sceneInfo);

        // 订单分账， 将冻结商户资金。
        if (isDivisionOrder(payOrder)) {
            JSONObject settleInfo = new JSONObject();
            settleInfo.put("profit_sharing", true);
            reqJSON.put("settle_info", settleInfo);
        }

        if (mchAppConfigContext.isIsvsubMch()) { // 特约商户
            WxpayIsvsubMchParams isvsubMchParams = (WxpayIsvsubMchParams) configContextQueryService
                    .queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), getIfCode());
            reqJSON.put("sp_appid", wxPayService.getConfig().getAppId());
            reqJSON.put("sp_mchid", wxPayService.getConfig().getMchId());
            reqJSON.put("sub_mchid", isvsubMchParams.getSubMchId());
            if (StringUtils.isNotBlank(isvsubMchParams.getSubMchAppId())) {
                reqJSON.put("sub_appid", isvsubMchParams.getSubMchAppId());
            }
        } else { // 普通商户
            reqJSON.put("appid", wxPayService.getConfig().getAppId());
            reqJSON.put("mchid", wxPayService.getConfig().getMchId());
        }
        return reqJSON;
    }

}
